package com.songqintuan.platform.security;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import net.sf.cglib.beans.BeanCopier;

import com.songqintuan.common.util.MessageFactory;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.AuthorityResources;
import com.songqintuan.model.vo.AuthorityResourcesVo;
import com.songqintuan.persist.service.hibernate.IAuthorityResourcesService;
import com.songqintuan.platform.base.IManagePojoPageBean;
import com.songqintuan.platform.base.ManagePojoPageBean;
import com.songqintuan.platform.common.constant.PlatformConstants;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;
import com.songqintuan.platform.page.AuthorityResourcesPage;
import com.songqintuan.platform.tree.ResourceDTree;
import com.songqintuan.platform.tree.ResourceTree;
import com.songqintuan.platform.util.EntityDataUtil;



@ManagedBean(name="manageAuthorityResourcesPage")
@SessionScoped
public class ManageAuthorityResourcesPageBean extends ManagePojoPageBean implements IManagePojoPageBean, Serializable {

	private static final long serialVersionUID = 5054087821033164829L;
	
	private AuthorityResourcesPage page;
	
	private AuthorityResourcesVo view;
	private AuthorityResourcesVo edit;
	
	//额外code ,不是通用code
    private ResourceTree rTree;
    
	//
	public ResourceTree getrTree() {
		return rTree;
	}

	public void setrTree(ResourceTree rTree) {
		this.rTree = rTree;
	}

	public AuthorityResourcesPage getPage() {
		return page;
	}

	public void setPage(AuthorityResourcesPage page) {
		this.page = page;
	}

	public AuthorityResourcesVo getView() {
		return view;
	}

	public void setView(AuthorityResourcesVo view) {
		this.view = view;
	}

	public AuthorityResourcesVo getEdit() {
		return edit;
	}

	public void setEdit(AuthorityResourcesVo edit) {
		this.edit = edit;
	}

	public ManageAuthorityResourcesPageBean(){
		
		AuthorityResources ob = new AuthorityResources();
		ob.setPid(new Long(0));
		page = new AuthorityResourcesPage(1,PlatformConstants.DEFAULT_ONE_PAGE_NUMBER,ob);
		page.getPageDatas();
		//初始化树
		if (null == this.getrTree()) {
			this.setrTree(new ResourceTree());
		}
	}

	public String selectPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (null != this.getPage().getPageNo()) {
			if (this.getPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
				return result;
			}
		}
		this.getPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getPage().getPageDatas();
		return result;
		
	}
	public String viewAction(){
		String result = null;
		clearErrorMessages();
		IAuthorityResourcesService<AuthorityResources,Long> objService = (IAuthorityResourcesService<AuthorityResources,Long>)SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.AUTHORITY_RESOURCES_SERVICE_BEAN_NAME);
		objService.setEntityClass(AuthorityResources.class);
		objService.setPKClass(Long.class);
		String objId= (String) SpringFacesUtil.getRequestParameter("objId");
		if (objId.equals("0")){
			return result;
		}
		AuthorityResources obj = objService.findById(new Long(objId));
		AuthorityResourcesVo objVo =new AuthorityResourcesVo();
		BeanCopier copyHere = BeanCopier.create(AuthorityResources.class, AuthorityResourcesVo.class, false);
		copyHere.copy(obj, objVo, null);
		this.setAction(PlatformConstants.ACTION_VIEW_TYPE);
		this.setView(objVo);
		return result;
	}
	public String editAction(){//get data
		String result = null;
		clearErrorMessages();
		IAuthorityResourcesService<AuthorityResources,Long> objService = (IAuthorityResourcesService<AuthorityResources,Long>)SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.AUTHORITY_RESOURCES_SERVICE_BEAN_NAME);
		objService.setEntityClass(AuthorityResources.class);
		objService.setPKClass(Long.class);
		String objId= (String) SpringFacesUtil.getRequestParameter("objId");
		AuthorityResources obj = objService.findById(new Long(objId));
		AuthorityResourcesVo objVo =new AuthorityResourcesVo();
		BeanCopier copyHere = BeanCopier.create(AuthorityResources.class, AuthorityResourcesVo.class, false);
		copyHere.copy(obj, objVo, null);
		this.setAction(PlatformConstants.ACTION_EDIT_TYPE);
		this.setEdit(objVo);
		return result;
	}
	public String viewToEditAction() {
		String result = null;
		clearErrorMessages();
		this.setEdit(this.getView());
		this.setView(null);
		this.setAction(PlatformConstants.ACTION_EDIT_TYPE);
		return result;
	}
	public String updateAction(){
		String result = null;
		try {
			this.clearErrorMessages();
			validator(this.getErrorMessages(),this.getEdit());
			if (!(this.getErrorMessages().isEmpty())) {
				return null;
			}
			IAuthorityResourcesService<AuthorityResources,Long> objService = (IAuthorityResourcesService<AuthorityResources,Long>)SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.AUTHORITY_RESOURCES_SERVICE_BEAN_NAME);
			objService.setEntityClass(AuthorityResources.class);
			objService.setPKClass(Long.class);
			AuthorityResources obj = objService.findById(this.getEdit().getId());
			obj.setName(this.getEdit().getName());
			obj.setUrl(this.getEdit().getUrl());
			obj.setLabel(this.getEdit().getLabel());
			obj.setPid(this.getEdit().getPid());
			obj.setRemark(this.getEdit().getRemark());
			obj.setType(this.getEdit().getType());
			obj.setSort(this.getEdit().getSort());
			//设置通用的修改数据属性 修改时间和修改人
			EntityDataUtil<AuthorityResources,Long> currUtil = new EntityDataUtil<AuthorityResources,Long>();
			currUtil.setModifyPropertyValue(obj);
			//end
			objService.saveOrUpdate(obj);
		}catch (Exception e) {
			String updateErrorN18 = MessageFactory.getMessage(PlatformConstants.USER_BUNDLE_NAME,"update.data.version.not.same.error.message", FacesMessage.SEVERITY_ERROR).getDetail();
			this.setEditDataInPageFlag(false);
			this.getErrorMessages().add(updateErrorN18);
			return result;
		} finally {
            //do nothing;//关闭session的时候还会报，并且在session那里抓取了所有的Exception。
        }
		return result;
	}
	public String preCreateAction(){
		String result = null;
		clearErrorMessages();
		this.setAction(PlatformConstants.ACTION_NEW_TYPE);
		String objId= (String) SpringFacesUtil.getRequestParameter("objId");
		AuthorityResourcesVo objVo =new AuthorityResourcesVo();
		objVo.setPid(new Long(objId));
		objVo.setType(PlatformConstants.RESOURCE_TYPE_ITEM);
		this.setEdit(objVo);
		return result;
	}
	
	public String preTopResourceCreateAction(){
		String result = null;
		clearErrorMessages();
		this.setAction(PlatformConstants.ACTION_NEW_TYPE);
		AuthorityResourcesVo objVo =new AuthorityResourcesVo();
		objVo.setPid(new Long(0));
		objVo.setType(PlatformConstants.RESOURCE_TYPE_GROUP);
		this.setEdit(objVo);
		return result;
		
	}
	
	public String saveAction(){//create new
		String result = null;
		clearErrorMessages();
		validator(this.getErrorMessages(),this.getEdit());
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		IAuthorityResourcesService<AuthorityResources,Long> objService = (IAuthorityResourcesService<AuthorityResources,Long>)SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.AUTHORITY_RESOURCES_SERVICE_BEAN_NAME);
		objService.setEntityClass(AuthorityResources.class);
		objService.setPKClass(Long.class);
		if (null != this.getEdit() && (this.getEdit().getPid() != 0) ) {
			AuthorityResources authorityResources = objService.findById(this.getEdit().getPid());//
			if (this.getEdit().getName().equals(authorityResources.getName())) {
				String AuthorityResourcesNotExistErrorN18 = MessageFactory.getMessage(PlatformConstants.USER_BUNDLE_NAME,"resources.name.same.error.message", FacesMessage.SEVERITY_ERROR).getDetail();
				this.getErrorMessages().add(AuthorityResourcesNotExistErrorN18);
			}
		}
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		AuthorityResources newObj = new AuthorityResources();
		BeanCopier copyHere = BeanCopier.create(AuthorityResourcesVo.class, AuthorityResources.class, false);
		copyHere.copy(this.getEdit(), newObj, null);
		objService.save(newObj);
		return result;
	}
	public String refreshPageAction(){
		String result = null;
		this.getPage().getPageDatas();
		return result;
	}
	public String searchPageAction(){
		return null;
	}
	private void validator(List<String> errorMessages,AuthorityResourcesVo eidt){
		/*String AuthorityResourcesSerialNumberN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"object.serialNumber.display", FacesMessage.SEVERITY_ERROR).getDetail();
		String AuthorityResourcesCodeDisplayN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"AuthorityResources.code.display", FacesMessage.SEVERITY_ERROR).getDetail();
		String AuthorityResourcesNameDisplayN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"AuthorityResources.name.display", FacesMessage.SEVERITY_ERROR).getDetail();
		ValidatorUtil.IntegerValidator(errorMessages, (null != edit.getSerialNumber())?edit.getSerialNumber().toString():"", AuthorityResourcesSerialNumberN18);
		ValidatorUtil.EmptyValidatorForInput(errorMessages, edit.getCode(), AuthorityResourcesCodeDisplayN18);
		ValidatorUtil.WhitespaceValidator(errorMessages, edit.getCode(), AuthorityResourcesCodeDisplayN18);
		ValidatorUtil.WhitespaceValidator(errorMessages, edit.getName(), AuthorityResourcesNameDisplayN18);*/
	}
}
